package com.linkoog.devtools.code_generator.action.model;

import com.linkoog.devtools.intellij.wizard.WizardStep;
import com.linkoog.devtools.code_generator.wizard.ModelWizardContext;
import com.linkoog.devtools.intellij.wizard.WizardDialog;
import com.linkoog.devtools.code_generator.wizard.model.GenModelFieldStep;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiManager;
import org.jetbrains.annotations.NotNull;

public class UiDesignerModelCreaterAction extends AnAction implements DumbAware {

    @Override
    public void actionPerformed(@NotNull AnActionEvent event) {
        Project project = event.getProject();
        if (project == null){
            return;
        }

        Module module = event.getData(LangDataKeys.MODULE);
        if (module == null){
            return;
        }

        VirtualFile selectedPackageVF = event.getData(LangDataKeys.VIRTUAL_FILE);
        if (selectedPackageVF == null || !selectedPackageVF.isDirectory()){
            return;
        }

        PsiDirectory basePsiDirectory = PsiManager.getInstance(project).findDirectory(selectedPackageVF);
        if (basePsiDirectory == null){
            return;
        }

        ModelWizardContext wizardContext = new ModelWizardContext();
        wizardContext.setProject(project);
        wizardContext.setModule(module);
        wizardContext.setSelectedPackageVF(selectedPackageVF);
        wizardContext.setBasePsiDirectory(basePsiDirectory);

        WizardDialog wizardDialog = new WizardDialog(project, wizardContext,"Create Model",new WizardStep[]{
                new GenModelFieldStep(project,wizardContext,  module, selectedPackageVF)
        });
        wizardDialog.show();
    }





}
